﻿<?xml version="1.0" encoding="utf-8"?>
<!-- 
Network Configuration for a generic pedestrian routing network
- This configuration incurs a runtime performance penalty by using the slower Script evaluators,
  but will work with more coordinate systems
  
OSM Tags:
    - highway:      pedestrian navigable roads
    - access:       road access restrictions
    - barrier:      barrier restrictions / time penalties
    - foot:         restrictions on foot traffic
-->
<networkConfiguration>
    <edge>
        <name>roads</name>
        <connect_policy>AnyVertex</connect_policy>
        <osm_fields>
            <osm_field>access</osm_field>
            <osm_field>foot</osm_field>
            <osm_field>name</osm_field>
        </osm_fields>
        <query>(highway IS NOT NULL) AND (LOWER(highway) in ('primary', 'primary_link, secondary', 'secondary_link', 'tertiary', 'tertiary_link', 'unclassified', 'residential', 'living_street', 'service', 'track', 'pedestrian', 'cycleway', 'footway', 'bridleway', 'steps', 'path'))</query>
        <street_name_fields>
            <direction_prefix></direction_prefix>
            <type_prefix></type_prefix>
            <street_name>osm_name</street_name>
            <direction_suffix></direction_suffix>
            <type_suffix></type_suffix>
        </street_name_fields>
    </edge>

    <junction>
        <name>barriers</name>
        <connect_policy>Override</connect_policy>
        <osm_fields>
            <osm_field>access</osm_field>
        </osm_fields>
        <query>(barrier IS NOT NULL)</query>
    </junction>

    <connectivity>
        <group>
            <source>roads</source>
            <source>barriers</source>
        </group>
    </connectivity>

    <directions>
        <length_attr>Length</length_attr>
        <length_units>Miles</length_units>
        <time_attr>WalkTime</time_attr>
    </directions>

    <network>
        <!-- Speed - descriptor -->
        <network_attribute>
            <name>Speed</name>
            <default_value>0</default_value>
            <descriptor>
                <datatype>double</datatype>
                <evaluator_attributes>
                    <source>roads</source>
                    <direction>From-To</direction>
                    <Field script_type="VBScript">
                        <expression>speed</expression>
                        <pre_logic>
                            <![CDATA[
                            speed = 6.0

                            'Adjust speed for road surface
                            Select Case LCase([osm_surface])
                                Case "compacted"
                                    speed = speed / 1.25
                                Case "metal"
                                    speed = speed / 1.50
                                Case "unpaved", "gravel", "fine_gravel", "pebblestone", "sand", "dirt", "grass"
                                    speed = speed / 2.00
                            End Select
                                
                            'Adjust for smoothness
                            Select Case LCase([osm_smoothness])
                                Case "intermediate"
                                    speed = speed / 1.25
                                Case "bad"
                                    speed = speed / 1.50
                                Case "very_bad"
                                    speed = speed / 1.75
                                Case "horrible"
                                    speed = speed / 2.00
                                Case "very_horrible"
                                    speed = speed / 3.00
                                Case "impassable"
                                    speed = speed / 5.00
                            End Select
                            ]]>
                        </pre_logic>
                    </Field>
                </evaluator_attributes>
                <evaluator_attributes>
                    <source>roads</source>
                    <direction>To-From</direction>
                    <Field script_type="VBScript">
                        <expression>speed</expression>
                        <pre_logic>
                            <![CDATA[
                            speed = 6.0

                            'Adjust speed for road surface
                            Select Case LCase([osm_surface])
                                Case "compacted"
                                    speed = speed / 1.25
                                Case "metal"
                                    speed = speed / 1.50
                                Case "unpaved", "gravel", "fine_gravel", "pebblestone", "sand", "dirt", "grass"
                                    speed = speed / 2.00
                            End Select
                                
                            'Adjust for smoothness
                            Select Case LCase([osm_smoothness])
                                Case "intermediate"
                                    speed = speed / 1.25
                                Case "bad"
                                    speed = speed / 1.50
                                Case "very_bad"
                                    speed = speed / 1.75
                                Case "horrible"
                                    speed = speed / 2.00
                                Case "very_horrible"
                                    speed = speed / 3.00
                                Case "impassable"
                                    speed = speed / 5.00
                            End Select
                            ]]>
                        </pre_logic>
                    </Field>
                </evaluator_attributes>
            </descriptor>
        </network_attribute>

        <!-- WalkTime - cost -->
        <network_attribute>
            <name>WalkTime</name>
            <default_value>0</default_value>
            <cost useAsDefault="true">
                <units>Minutes</units>
                <datatype>double</datatype>
                <evaluator_attributes>
                    <source>roads</source>
                    <direction>From-To</direction>
                    <Script script_type="VBScript">
                        <expression>walkTime</expression>
                        <pre_logic>
                            <![CDATA[
                            'Speed must not be 0 (division by 0).
                            speed = Edge.AttributeValueByName("Speed")
                            If speed = 0 Then
                                speed = 0.001
                            End If

                            walkTime = (Edge.AttributeValueByName("Length") * 60) / (speed * 1000)
                            ]]>
                        </pre_logic>
                    </Script>
                </evaluator_attributes>
                <evaluator_attributes>
                    <source>roads</source>
                    <direction>To-From</direction>
                    <Script script_type="VBScript">
                        <expression>walkTime</expression>
                        <pre_logic>
                            <![CDATA[
                            'Speed must not be 0 (division by 0).
                            speed = Edge.AttributeValueByName("Speed")
                            If speed = 0 Then
                                speed = 0.001
                            End If

                            walkTime = (Edge.AttributeValueByName("Length") * 60) / (speed * 1000)
                            ]]>
                        </pre_logic>
                    </Script>
                </evaluator_attributes>
                <evaluator_attributes>
                    <source>barriers</source>
                    <direction>To-From</direction>
                    <Field script_type="VBScript">
                        <expression>walkTime</expression>
                        <pre_logic>
                            <![CDATA[
                            walkTime = 0
                            
                            Select Case LCase([highway])
                            Case "stop", "traffic_signals"
                                walkTime = walkTime + 0.1
                            End Select

                            If NOT IsNull([barrier]) Then
                                walkTime = walkTime + 0.1
                            End If
                            ]]>
                        </pre_logic>
                    </Field>
                </evaluator_attributes>
            </cost>
        </network_attribute>

        <!-- Length - cost -->
        <network_attribute>
            <name>Length</name>
            <default_value>0</default_value>
            <cost useAsDefault="false">
                <units>Meters</units>
                <datatype>double</datatype>
                <evaluator_attributes>
                    <source>roads</source>
                    <direction>From-To</direction>
                    <Field script_type="VBScript">
                        <expression>[SHAPE]</expression>
                    </Field>
                </evaluator_attributes>
                <evaluator_attributes>
                    <source>roads</source>
                    <direction>To-From</direction>
                    <Field script_type="VBScript">
                        <expression>[SHAPE]</expression>
                    </Field>
                </evaluator_attributes>
            </cost>
        </network_attribute>

        <!-- Access - restriction -->
        <network_attribute>
            <name>Access</name>
            <default_value>false</default_value>
            <restriction useAsDefault="true">
                <evaluator_attributes>
                    <source>roads</source>
                    <direction>From-To</direction>
                    <Field script_type="VBScript">
                        <expression>restricted</expression>
                        <pre_logic>
                            <![CDATA[
                            restricted = False
                            Select Case LCase([osm_access])
                                Case "no", "destination", "delivery", "agricultural", "forestry", "private"
                                    restricted = True
                            End Select

                            If restricted = False Then
                                If LCase([osm_foot]) = "no" Then
                                    restricted = True
                                End If
                            End If
                            ]]>
                        </pre_logic>
                    </Field>
                </evaluator_attributes>
                <evaluator_attributes>
                    <source>roads</source>
                    <direction>To-From</direction>
                    <Field script_type="VBScript">
                        <expression>restricted</expression>
                        <pre_logic>
                            <![CDATA[
                            restricted = False
                            Select Case LCase([osm_access])
                                Case "no", "destination", "delivery", "agricultural", "forestry", "private"
                                    restricted = True
                            End Select

                            If restricted = False Then
                                If LCase([osm_foot]) = "no" Then
                                    restricted = True
                                End If
                            End If
                            ]]>
                        </pre_logic>
                    </Field>
                </evaluator_attributes>
                <evaluator_attributes>
                    <source>barriers</source>
                    <direction>From-To</direction>
                    <Field script_type="VBScript">
                        <expression>restricted</expression>
                        <pre_logic>
                            <![CDATA[
                            restricted = True
                            Select Case LCase([osm_access])
                                Case "yes", "designated", "official", "permissive"
                                    restricted = False
                            End Select
                            ]]>
                        </pre_logic>
                    </Field>
                </evaluator_attributes>
            </restriction>
        </network_attribute>
    </network>
</networkConfiguration>
